﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Framework;
using TjZheng.SSO.Business;
using TjZheng.SSO.DataEntity;
using TjZheng.Cache;

namespace TjZheng.SSO.Process
{
    public partial class UserProcess
    {
        /// <summary>
        /// 获取并转换用户数据
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public virtual InvokeResult ConvertUserInfo(string userName, string password)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            BizUserInfo objBizUserInfo = new BizUserInfo();
            UserInfo objUserInfo = objBizUserInfo.GetRecordCache("", TimeSpan.FromSeconds(5), "UserName=@UserName", "UserName".CreateSqlParameters(userName));
            objUserInfo.Password = objUserInfo.Password.EncodeMD5(objUserInfo.Salt);
            if (!string.Equals(objUserInfo.Password, objUserInfo.Password))
            {
                objInvokeResult.ResultCode = "PasswordNotMatch";
                objInvokeResult.ResultMessage = "对不起，用户密码错误";
                return objInvokeResult;
            }
            objInvokeResult.Data = objUserInfo.UserId;
            return objInvokeResult;
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public virtual InvokeResult LoginIn(string userName, string password)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (userName.IsNull())
            {
                objInvokeResult.ResultCode = "UserNameNull";
                objInvokeResult.ResultMessage = "对不起，用户名不能为空";
                return objInvokeResult;
            }
            if (password.IsNull())
            {
                objInvokeResult.ResultCode = "PasswordNull";
                objInvokeResult.ResultMessage = "对不起，用户密码不能为空";
                return objInvokeResult;
            }
            userName = userName.Trim();
            objInvokeResult = ConvertUserInfo(userName, password);
            if (objInvokeResult.ResultCode != "Success")
            {
                return objInvokeResult;
            }
            string userId = objInvokeResult.Data.ToString();
            BizTempInfo objBizTempInfo = new BizTempInfo();
            TempInfo objTempInfo = objBizTempInfo.GetRecordCache("", TimeSpan.FromSeconds(5), "ObjectId=@ObjectId AND ObjectCode='UserInfo'", "ObjectId".CreateSqlParameters(userId));
            if (objTempInfo == null)
            {
                objTempInfo = new TempInfo
                {
                    ObjectCode = "UserInfo",
                    ObjectId = userId,
                    EffectiveDate = DateTime.Now.AddHours(2)
                };
                objBizTempInfo.Add(objTempInfo);
            }
            else
            {
                if (objTempInfo.EffectiveDate.IsLessThanCurrent())
                {
                    objTempInfo.EffectiveDate = DateTime.Now.AddHours(2);
                    objBizTempInfo.Update(objTempInfo);
                }
            }
            objInvokeResult.ResultMessage = "登录成功";
            objInvokeResult.Data = userId;
            return objInvokeResult;
        }

    }
}
